
public class Util {

    public static double calculateDistance(Intersection begin, Intersection end) {
        double lat1 = Math.toRadians(begin.latitude);
        double long1 = Math.toRadians(begin.longitude);
        double lat2 = Math.toRadians(end.latitude);
        double long2 = Math.toRadians(end.longitude);
        double d1 = (Math.sin((lat2 - lat1) / 2) * Math.sin((lat2 - lat1) / 2)) + (Math.cos(lat1) * Math.cos(lat2) * Math.sin((long2 - long1) / 2) * Math.sin((long2 - long1) / 2));
        double res = 2 * Math.atan2(Math.sqrt(d1), Math.sqrt(1 - d1));
        return 6371000 * res;
    }
}
